package com.sphd.Restaurant.utils;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.sphd.Restaurant.base.MyApplication;
import com.sphd.Restaurant.interfFace.VolleyCallBack;


import java.util.HashMap;
import java.util.Map;

/**
 * Created by Hu on 2016/9/27.
 */

public class VolleyUtil {
    VolleyCallBack volleyCallBack;
    public void setStringRequest(String url, VolleyCallBack volleyCallBack) {
        Log.e("volley String=", url);
        this.volleyCallBack = volleyCallBack;
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
            volleyCallBack.onResponse(response);
            Log.e("volley response=", response);
        }, error -> {
            volleyCallBack.onErrorResponse(error);
            Log.e("volley error=", error.toString());
        });
        MyApplication.getRequestQueue().add(stringRequest);
    }

    public void setStringRequest(String url, VolleyCallBack volleyCallBack, Map<String, String> map) {
        Log.e("volley String=", url);
        Log.e("volley map=", map.toString());

        this.volleyCallBack = volleyCallBack;
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response ->
                volleyCallBack.onResponse(response),
                error ->
                        volleyCallBack.onErrorResponse(error)) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        MyApplication.getRequestQueue().add(stringRequest);
    }

    public void setJsonObjectRequest(String url, VolleyCallBack volleyCallBack, Map<String, String> map) {
        this.volleyCallBack = volleyCallBack;
        Log.e("volley jsobj=", url);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url, response -> {
            Log.e("volley json=", response.toString());
            volleyCallBack.onResponse(response);
        }, error -> {
            Log.e("volley error=", error.toString());
            volleyCallBack.onErrorResponse(error);
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map1 =new HashMap<>();
                map1.putAll(map);
                Log.e("map1",map1.toString());
                return map1;
            }
        };
        MyApplication.getRequestQueue().add(jsonObjectRequest);
    }

    public void setJsonObjectRequest(String url, VolleyCallBack volleyCallBack) {
        this.volleyCallBack = volleyCallBack;
        Log.e("volley jsobj=", url);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,url, response -> {
            Log.e("volley json=", response.toString());
            volleyCallBack.onResponse(response);
        }, error -> {
            Log.e("volley error=", error.toString());
            volleyCallBack.onErrorResponse(error);
        });
        MyApplication.getRequestQueue().add(jsonObjectRequest);
    }

    //
    public void setJsonArrayRequest(String url, VolleyCallBack volleyCallBack) {
        Log.e("volley jsobarray=", url);
        this.volleyCallBack = volleyCallBack;
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, response ->
                volleyCallBack.onResponse(response), error ->
                volleyCallBack.onErrorResponse(error));
        MyApplication.getRequestQueue().add(jsonArrayRequest);
    }

    public void setJsonArrayRequest(String url, VolleyCallBack volleyCallBack, Map<String, String> map) {
        Log.e("volley jsobarray=", url);
        this.volleyCallBack = volleyCallBack;
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, response ->
                volleyCallBack.onResponse(response), error ->
                volleyCallBack.onErrorResponse(error)) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        MyApplication.getRequestQueue().add(jsonArrayRequest);
    }


}
